package com.meta.util;


import com.meta.common.ConstDef;
import com.meta.util.vo.DateOffset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTool {

    public static final Date MAX_LIFE_DATE = parseDate("3000-01-01");//终身寿险

    private final static Logger logger = LoggerFactory.getLogger(DateTool.class);

    //日期格式
    public static final String DATE_FORMAT_DEFAULT = ConstDef.DATE_FORMAT_DEFAULT;
    public static final String DATETIME_FORMAT_DEFAULT = ConstDef.DATETIME_FORMAT_DEFAULT;


    public static SimpleDateFormat getSimpleDateFormat() {

        return new SimpleDateFormat(DATE_FORMAT_DEFAULT);
    }

    public static SimpleDateFormat getSimpleDatetimeFormat() {
        return new SimpleDateFormat(DATETIME_FORMAT_DEFAULT);
    }


    public static Date parseDate(String value) {
        if (value == null || value.equals("") || value.equals("null"))
            return null;

        try {
            return getSimpleDateFormat().parse(value);
        } catch (Exception e) {
            logger.info(String.valueOf(e.equals(e.getMessage())));
            throw new RuntimeException("无效的日期格式：" + value);
        }
    }

    public static Date parseDateForYYYYMMdd(String value ) {

        if ( value == null || value.equals("") || value.equals("null"))
            return null;
        SimpleDateFormat sdfYYYYMMdd= new SimpleDateFormat("yyyyMMdd");
        try {
            return sdfYYYYMMdd.parse(value);
        }catch (Exception e ) {
            logger.info(String.valueOf(e.equals(e.getMessage())));
            throw new RuntimeException("无效的日期格式：" + value);
        }
    }

    public static Date parseDatetime(String value) {
        if (value == null || value.equals("") || value.equals("null"))
            return null;

        try {
            return getSimpleDatetimeFormat().parse(value);
        } catch (Exception e) {
            throw new RuntimeException("无效的日期时间格式[" + DATETIME_FORMAT_DEFAULT + "]：[" + value +"]");
        }
    }


    public static String formatDate(Date date) {
        if (date == null)
            return null;
        else
            return getSimpleDateFormat().format(date);
    }


    public static String formatDatetime(Date date) {
        if (date == null)
            return null;
        return getSimpleDatetimeFormat().format(date);
    }

    public static Date getSystemTime() {
        return new Date();
    }

    public static Date getSystemDate(){
        return trimDate(new Date());
    }

    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        return month;
    }

    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    public static Date trimDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DEFAULT);
        String s = sdf.format(date);

        try {
            return sdf.parse(s);
        } catch (ParseException e) {
            logger.error("数据解析异常 : "+ e.getMessage());
        }
        return null;
    }

    public static  Calendar date2Calendar(Date date){
        Calendar calendar = Calendar. getInstance();
        calendar.setTime(date);
        return calendar;
    }



    public static int dateOffset( Date startDate, Date endDate ){
        startDate = trimDate(startDate);
        endDate = trimDate(endDate);
        Calendar c1 = Calendar. getInstance();
        c1.setTime(startDate);
        Calendar c2= Calendar. getInstance();
        c2.setTime(endDate);

        if ( c1.compareTo(c2) > 0 )
            throw new RuntimeException("开始日期不能大于结束日期！");

       long days =  (c2.getTimeInMillis() - c1.getTimeInMillis())/(1000*60*60*24);

       return (int) days;

    }

    public static boolean isLeapYear( int year ){
        if ( (year % 4 == 0 && year % 100 != 0 ) || (year%400==0) )
            return true;
        else
            return false;
    }

    //下面的判断是假设startDate和endDate不超过1年，可以等于1年，如果大于一年，只看最后一年是否包含2月29日
    public static boolean checkFeb29InTerm( Date startDate, Date endDate){
        Calendar c1 = Calendar. getInstance();
        c1.setTime(trimDate(startDate));
        Calendar c2 = Calendar. getInstance();
        c2.setTime(trimDate(endDate));
        Calendar c3 = Calendar. getInstance();
        c3.setTime(c2.getTime());
        c3.add(Calendar.YEAR,-1);
        if ( c1.compareTo(c3) < 0 )
            c1.setTime(c3.getTime());

        if (isLeapYear(c1.get(Calendar.YEAR))){
            c3.set(c1.get(Calendar.YEAR),2,29);
        }
        else if ( isLeapYear(c2.get(Calendar.YEAR)))
            c3.set(c2.get(Calendar.YEAR),2,29);
        else
            return false;

        return  c1.compareTo(c3) <=0 && c2.compareTo(c3) > 0 ;
    }

    public static int totalDaysOfYear( Date startDate, Date endDate){
        if ( checkFeb29InTerm (startDate, endDate))
            return 366;
        else
            return 355;
    }

    public static Date addDate(Date date, int days){
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE,days);
            return cal.getTime();
    }

    public static Date addDateByMonth(Date date, int month){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH,month);
        return cal.getTime();
    }

    public static Date addDateByYear(Date date, int year){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR,year);
        return cal.getTime();
    }

    public static Date addDate(Date date, int days, int month, int year){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,days);
        cal.add(Calendar.MONTH,month);
        cal.add(Calendar.YEAR,year);
        return cal.getTime();
    }




    public static Date getLastDateOfMonth( int year, int month){
        month = month-1;
        Calendar cal = Calendar.getInstance();
        cal.set(year,month,1,0,0,0);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(year,month,lastDay,0,0,0);
        return trimDate(cal.getTime());
    }

    public static Date getFirstDateOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1, 0, 0, 0); // 设置为2023年3月21日
        Date date = calendar.getTime();
        return trimDate(date);
    }

    public static DateOffset parseDateOffset(Date date){
        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();
        // 使用Date对象设置Calendar的时间
        calendar.setTime(date);
        // 使用Calendar的get()方法获取年份
        DateOffset offset = new DateOffset();
        offset.setYear(calendar.get(Calendar.YEAR));
        offset.setMonth(calendar.get(Calendar.MONTH));
        offset.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        return offset;
    }
}